########################################################
######  Title: Sharia, Democracy, and Militancy  #######
######  Date: June 9, 2016                       #######
########################################################

## Note: Set your working directory

## Load packages

library("ggplot2")
library("gridExtra")
library("foreign")


## Upload datasets for figures

# Note: this file can only open "FMS-four provinces data-ANALYSIS.dta" that is created by STATA version 12 and below.
# To solve this problem, you can read "FMS-four provinces data-ANALYSIS.dta" in Stata 14 and save it in the older STATA 12 format.

pk <- read.dta("FMS-four provinces data-ANALYSIS.dta", convert.factor = FALSE)

pk.endorse <- read.csv("pkdata.csv")


##########################################
#### FIGURE 1: CONCEPTIONS OF SHARIA ####
##########################################

# (a) Support for Sharia Items: Provides

support.provides <- c(85.71, 90.24, 90.42, 91.03, 89.21)
vars.provides <- c("Provides services", "Non-corrupt", "Justice system", "Provides security", "Mean of provides items")

data <- data.frame()

sharia.provides <- ggplot(data = data, aes(y = support.provides, x = vars.provides)) +
  coord_cartesian(ylim = c(0, 100)) +
  geom_bar(stat = "identity", fill=c("black", "black", "black", "black", "gray47"), width=.65) +
  ylab("Percentage") +
  ggtitle("(a) Shari`a as Providing") +
  theme_bw() +    
  theme(axis.line.x = element_line(color = "black", size = .35),
        axis.line.y = element_line(color = "black", size = .35),
        axis.ticks = element_line(size = .35),
        axis.title.x = element_blank(), axis.title.y = element_text(size=9),
        axis.text.x=element_text(angle=30, hjust=1, size = 7.25, color = c("black", "black", "black", "black", "gray47")),
        axis.text.y=element_text(size = 7),
        plot.title = element_text(size = 9),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank()) +
  scale_x_discrete(limits=c("Provides services", "Non-corrupt", "Justice system", "Provides security", "Mean of provides items")) +
  scale_y_continuous(expand = c(0, 0))
ggsave(filename = "sharia.provides.pdf", width = 5, height = 4)


# (b) Support for Sharia Items: Imposes

support.imposes <- c(43.74, 60.34, 52.1)
vars.imposes <- c("Restrict women", "Hudud Punishment", "Mean of imposes items")

data <- data.frame()

sharia.imposes <- ggplot(data = data, aes(y = support.imposes, x = vars.imposes)) +
  coord_cartesian(ylim = c(0, 100)) +
  geom_bar(stat = "identity", fill=c("black", "black", "gray47"), width=.65) +
  ylab("Percentage") +
  ggtitle("(b) Shari`a as Imposing") +
  theme_bw() +    
  theme(axis.line.x = element_line(color="black", size = .35),
        axis.line.y = element_line(color="black", size = .35),
        axis.ticks = element_line(size = .35),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        axis.text.x=element_text(angle=30, hjust=1, size = 7.25, color = c("black", "black", "gray47")),
        axis.text.y=element_text(size = 7),
        plot.title = element_text(size = 9),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank()) +
  scale_x_discrete(limits=c("Restrict women", "Hudud Punishment", "Mean of imposes items")) +
  scale_y_continuous(expand = c(0, 0))
ggsave(filename = "sharia.imposes.pdf", width = 5, height = 4)

# Combine and Save as PDF

pdf(file = "figure1.pdf", height = 2.75, width = 4.5)
figure1 <- grid.arrange(sharia.provides, sharia.imposes, ncol=2, widths=c(.6, .4))
dev.off()


#################################################
## FIGURE 2: SHARIA AND SUPPORT FOR DEMOCRACY ##
#################################################

figure2 <- ggplot(data = pk[], aes(x = democracy)) +
  geom_histogram(aes(y=..density..), binwidth = 0.05, fill = "black", na.rm=TRUE) +
  geom_density(na.rm=TRUE) +
  xlab("Democracy Index") +
  ylab("Density") +        
  theme(axis.title.x = element_text(size=9), axis.title.y = element_text(size=9),
        axis.text.x=element_text(size = 8),
        axis.text.y=element_text(size = 8))    
ggsave(filename = "figure2.pdf", width = 4.5, height = 3.5)


######################################
## FIGURE 3: SUPPORT FOR MILITANCY ##
######################################

# Distributions from Direct Questions

ssp <- c(45.78, 16.59, 18.69, 9.54, 9.40)
atal <- c(55.24, 13.96, 15.69, 7.51, 7.61) 
mil.cat <- c("Not at all", "A little", "A moderate amount", "A lot", "A great deal")

# SSP

data <- data.frame()

figure.ssp <- ggplot(data = data, aes(x = mil.cat, y = ssp)) +
  geom_bar(stat = "identity", fill="black") +
  ylab("Percentage") +
  ggtitle("(a) Support for SSP") +
  theme_bw() +    
  theme(axis.line.x = element_line(color="black", size = .35),
        axis.line.y = element_line(color="black", size = .35),
        axis.ticks = element_line(size = .35),
        axis.title.x = element_blank(), axis.title.y = element_text(size=9),
        axis.text.x=element_text(angle=30, hjust=1, size = 7.25, color = "black"),
        axis.text.y=element_text(size = 7),
        plot.title = element_text(size = 9),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank()) +
  scale_x_discrete(limits=c("Not at all", "A little", "A moderate amount", "A lot", "A great deal")) +
  scale_y_continuous(limits=c(0, 60), expand = c(0, 0)) +
ggsave(filename = "figure.ssp.pdf", width = 4, height = 4)

# Afghan Taliban

data <- data.frame()

figure.atal <- ggplot(data = data, aes(x = mil.cat, y = atal)) +
  geom_bar(stat = "identity", fill="black") +
  ggtitle("(b) Support for Afghan Taliban") +
  theme_bw() +    
  theme(axis.line.x = element_line(color = "black", size = .35),
        axis.line.y = element_line(color="black", size = .35),
        axis.ticks = element_line(size = .35),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        axis.text.x=element_text(angle=30, hjust=1, size = 7.25, color = "black"),
        axis.text.y=element_text(size = 7),
        plot.title = element_text(size = 9),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank()) +
  scale_x_discrete(limits=c("Not at all", "A little", "A moderate amount", "A lot", "A great deal")) +
  scale_y_continuous(limits=c(0, 60), expand = c(0, 0)) +
ggsave(filename = "figure.atal.pdf", width = 5, height = 4)

# Endorsement experiment results

# Calculate 95% confidence intervals

pk.endorse$overall.ci.lower <- pk.endorse$overall.coef - (qnorm(.975) * pk.endorse$overall.se)
pk.endorse$overall.ci.upper <- pk.endorse$overall.coef + (qnorm(.975) * pk.endorse$overall.se)

figure.endorse <- ggplot(data = pk.endorse[-5,], aes(x = group, y = overall.coef)) +
  coord_cartesian(ylim = c(-.08, .08)) +
  geom_point(size = 2.5, color = c("gray47", "black", "black", "black")) +
  geom_hline(aes(yintercept=0), colour="darkgrey", linetype="dashed") +
  ylab("Endorsement Effect") +
  ggtitle("(c) Endorsement Experiment") +
  theme_bw() +    
  theme(axis.line.x = element_line(color = "black", size = .35),
        axis.line.y = element_line(color="black", size = .35),
        axis.ticks = element_line(size = .35),
        axis.title.x = element_blank(), axis.title.y = element_text(size=9),
        axis.text.x=element_text(angle=30, hjust=1, size = 7.25, color=c("black", "black", "black", "gray47")),
        axis.text.y=element_text(size = 7),
        plot.title = element_text(size = 9),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank()) +
  scale_x_discrete(limits=c("ssp", "pak.tal", "afghan.tal", "militant.groups"),
                   labels = c("Sipah-e-Sahaba \n Pakistan",
                              "Pakistan Taliban", "Afghan Taliban", "Militant Group \n Average")) +
  geom_errorbar(aes(ymin=overall.ci.lower, ymax=overall.ci.upper, width=.2), color = c("gray47", "black", "black", "black")) +
  ggsave(filename = "figure.endorse.pdf", width = 5, height = 4)

# Save as pdf

pdf(file = "figure3.pdf", height = 5.5, width = 4.5)
figure3 <- grid.arrange(arrangeGrob(figure.ssp, figure.atal, ncol=2, widths = c(.525, .475)), figure.endorse, ncol=1)
dev.off()


